using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;

using My.IdlConverter.Core.Types;
using My.IdlConverter.Core;

namespace My.IdlConverter.Com
{
    class ComParameterTypeFormatStrategy : ParameterTypeFormatStrategy
    {
        public override string Format(IType type)
        {
            if (type == null)
                throw new ArgumentNullException("The type should no be null.");

            string prefix = type.Const ? "const " : "";
            string suffix = type.Ref ? "*" : "";
            StringBuilder sb = new StringBuilder();

            TypeMap map = type.Substitution;
            foreach (TypeMap.TypeSubstitution nextSubstitution in map.SubstitutionTypes)
            {
                PtrType subType = nextSubstitution.Type as PtrType;
                if (subType != null)
                {
                    string interfaceName = Component.FindInterfaceBy((type.Parent as Parameter).Parent);
                    if (interfaceName.Length > 0)
                    {
                        if (prefix.Equals("const"))
                            sb.Append(prefix + interfaceName + suffix);
                        else
                        {
                            sb.Append(interfaceName + "**");
                        }
                        continue;
                    }
                }
                string literal = nextSubstitution.Literal.Replace("Iv_DOM", "I");
                if ((literal.Equals("BSTR")) && (prefix.Length > 0))
                {
                    sb.Append(literal);
                }
                else
                    sb.Append(prefix + literal + suffix);
            }

            return sb.ToString();
        }
    }
}
